package com.yanqu.road.dao.impl.activity.douluodalu;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.douluodalu.data.DouLuoDaLuPatronsTimesData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class DouLuoDaLuPatronsTimesDataDaoImpl extends TempDataObjectDao<DouLuoDaLuPatronsTimesData> {


    public static String getDouLuoDaLuPatronsTimesDataSql = "select * from `t_u_douluodalu_patrons_time` where `activityId`=? and `userId` =?";
    public Map<Integer, DouLuoDaLuPatronsTimesData> getDouLuoDaLuPatronsTimesData(int activityId, long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, DouLuoDaLuPatronsTimesData> map = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getDouLuoDaLuPatronsTimesDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setLong(i++, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                DouLuoDaLuPatronsTimesData data = parse(rs);
                map.put(data.getPatronsId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    public static String insertDouLuoDaLuPatronsTimesDataSql = "insert into `t_u_douluodalu_patrons_time`(activityId,userId,patronsId,sendTimes,fightTimes,zhuiBuTimes,resetTime) values (?,?,?,?,?,?,?)";

    @Override
    protected boolean addData(DouLuoDaLuPatronsTimesData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getPatronsId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getSendTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getFightTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getZhuiBuTimes()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getResetTime()));
        if (getDb().executeNoneQuery(insertDouLuoDaLuPatronsTimesDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String updateDouLuoDaLuPatronsTimesDataSql = "update `t_u_douluodalu_patrons_time` set sendTimes=?,fightTimes=?,zhuiBuTimes=?,resetTime=? where activityId=? and userId=? and patronsId=?";

    @Override
    protected boolean updateData(DouLuoDaLuPatronsTimesData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getSendTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getFightTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getZhuiBuTimes()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getResetTime()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getPatronsId()));
        if (getDb().executeNoneQuery(updateDouLuoDaLuPatronsTimesDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected DouLuoDaLuPatronsTimesData parseData(ResultSet rs) throws SQLException {
        DouLuoDaLuPatronsTimesData data = new DouLuoDaLuPatronsTimesData();
        data.setActivityId(rs.getInt("activityId"));
        data.setUserId(rs.getLong("userId"));
        data.setPatronsId(rs.getInt("patronsId"));
        data.setSendTimes(rs.getInt("sendTimes"));
        data.setFightTimes(rs.getInt("fightTimes"));
        data.setZhuiBuTimes(rs.getInt("zhuiBuTimes"));
        data.setResetTime(rs.getLong("resetTime"));

        return data;
    }
}
